package com.newasia.server.common.checkHandler;

import com.newasia.server.common.exception.ValidateException;
import com.newasia.server.common.result.ManagementRetCode;
import com.newasia.server.controller.form.UserForm;
import jodd.util.StringUtil;

/**
 * @author LiChong
 * @description 密码检测
 * @date 2020/4/2
 */

public class PasswordCheckHandler extends CheckHandler {

    @Override
    public void doHandler(UserForm form) {
        String password = form.getPassword();
        if(StringUtil.isEmpty(password)){
            throw new ValidateException(
                    ManagementRetCode.USERORPASSWORD_NOT_EXIST.getCode(),
                    ManagementRetCode.USERORPASSWORD_NOT_EXIST.getMessage());
        }
        String pwd = "^[A-Za-z0-9]{4,40}$";
        if (!password.matches(pwd)) {
            throw new ValidateException(
                    ManagementRetCode.ILLEGAL_PASSWORD.getCode(),
                    ManagementRetCode.ILLEGAL_PASSWORD.getMessage());
        }
        if (password.length()<4) {
            throw new ValidateException(
                    ManagementRetCode.PASSWORD_TOO_SHORT.getCode(),
                    ManagementRetCode.PASSWORD_TOO_SHORT.getMessage());
        }
        if (password.length()>15) {
            throw new ValidateException(
                    ManagementRetCode.PASSWORD_TOO_LONG.getCode(),
                    ManagementRetCode.PASSWORD_TOO_LONG.getMessage());
        }
        if(null != next) {
            next.doHandler(form);
        }
    }
}
